<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\lib\Layui;

use app\admin\logic\Problem as LogicProblem;

use app\admin\validate\{
    Problem as ProblemValidate,
    ProblemClassify as ProblemClassifyValidate
};

/**
 * 常见问题及分类
 */
class Problem
{
    // +-------------------------------------------------
    // | 常见问题
    // +-------------------------------------------------

    /**
     * 问题列表
     */
    public function index()
    {
        return view();
    }

    /**
     * 常见问题数据表格
     */
    public function data(int $page = 1, int $limit = 10)
    {
        $data = LogicProblem::getLists($page, $limit);
        return Layui::table('常见问题', ...array_values($data));
    }

    /**
     * 添加问题
     *
     * @param array $data
     */
    public function add(array $data = [])
    {
        if (request()->isPost()) {
            try {
                validate(ProblemValidate::class)->check($data);
                LogicProblem::save($data);
            } catch (\Exception $e) {
                return wrong($e->getMessage());
            }
            return success('添加成功');
        } else {
            return view();
        }
    }

    /**
     * 删除问题
     *
     * @param int|array $id
     */
    public function delete($id)
    {
        try {
            LogicProblem::delete($id);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return success('删除成功');
    }

    /**
     * 编辑问题
     *
     * @param integer $id
     */
    public function read(int $id)
    {
        try {
            $data = LogicProblem::read($id);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return view('', compact('data'));
    }

    /**
     * 更新问题
     *
     * @param integer $id
     * @param array   $data
     */
    public function update(int $id, array $data)
    {
        try {
            validate(ProblemValidate::class)->check($data);
            LogicProblem::update($id, $data);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return success('修改成功');
    }

    // +----------------------------------------------------------------------
    // | 常见问题分类
    // +----------------------------------------------------------------------

    /**
     * 问题分类
     */
    public function classify()
    {
        return view();
    }

    /**
     * 问题分类数据表格
     */
    public function dataClassify(int $page = 1, int $limit = 10)
    {
        $data = LogicProblem::getClassifyLists($page, $limit);
        return Layui::table('问题分类', ...array_values($data));
    }

    /**
     * 添加分类
     *
     * @param array $data
     */
    public function addClassify(array $data = [])
    {
        if (request()->isPost()) {
            try {
                validate(ProblemClassifyValidate::class)->check($data);
                LogicProblem::saveClassify($data);
            } catch (\Exception $e) {
                return wrong($e->getMessage());
            }
            return success('添加成功');
        } else {
            return view();
        }
    }

    /**
     * 删除分类
     *
     * @param int|array $id
     */
    public function deleteClassify($id)
    {
        try {
            LogicProblem::deleteClassify($id);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return success('删除成功');
    }

    /**
     * 编辑分类
     *
     * @param integer $id
     */
    public function readClassify(int $id)
    {
        try {
            $data = LogicProblem::readClassify($id);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return view('', compact('data'));
    }

    /**
     * 更新分类
     *
     * @param integer $id
     * @param array   $data
     */
    public function updateClassify(int $id, array $data)
    {
        try {
            validate(ProblemClassifyValidate::class)->check($data);
            LogicProblem::updateClassify($id, $data);
        } catch (\Exception $e) {
            return wrong($e->getMessage());
        }
        return success('修改成功');
    }

    /**
     * 选择分类
     *
     * @param integer $id 分类id
     */
    public function selectClassify(int $id = 0)
    {
        $data = LogicProblem::selectClassify($id);
        return data($data, '选择分类');
    }
}
